package com.sg.service.base.framework.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sg.common.util.CommonFunctionHelper;
import com.sg.dto.base.framework.req.BatchQueryTypeListReq;
import com.sg.dto.base.framework.req.QueryTypeDetailReq;
import com.sg.dto.base.framework.req.QueryTypeListReq;
import com.sg.entity.MetadataType;
import com.sg.service.base.framework.MMetadataTypeService;
import com.wicket.okrframework.common.dto.FieldInfo;
import com.wicket.okrframework.common.exception.BizException;
import com.wicket.okrframework.dal.mapper.mbg.MetadataTypeMapper;
import org.apache.skywalking.apm.toolkit.trace.Tag;
import org.apache.skywalking.apm.toolkit.trace.Tags;
import org.apache.skywalking.apm.toolkit.trace.Trace;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author ：
 * @version 1.0
 * @since 2022/1/19 15:39
 */
@Service
public class MMetadataTypeServiceImpl
        extends ServiceImpl<MetadataTypeMapper, MetadataType>
        implements MMetadataTypeService {

    @Autowired
    MetadataTypeMapper metadataTypeMapper;

    /**
     * 1-1-05批量查询类型列表[655]
     * gen by moon at 10/15/2022, 5:37:43 PM
     */
    @Trace(operationName = "1-1-05批量查询类型列表")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<MetadataType> batchQueryTypeList(BatchQueryTypeListReq reqDto) {

        try {
            QueryWrapper<MetadataType> queryWrapper = new QueryWrapper<>();
            reqDto.setIsValid("FALSE");
            if (reqDto.getTypeList() == null || reqDto.getTypeList().isEmpty()) {
                return new ArrayList<>();
            }
            queryWrapper.in("type_id", reqDto.getTypeList());
            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(reqDto);
            List<FieldInfo> compareList = CommonFunctionHelper.getRangeField(MetadataType.class);
            CommonFunctionHelper.buildQueryWrapperCompare(queryWrapper, compareList, fieldInfoMap);
            List<FieldInfo> likeList = CommonFunctionHelper.getLikeFiled(MetadataType.class);
            CommonFunctionHelper.buildQueryWrapperLike(queryWrapper, likeList, fieldInfoMap);
            likeList.addAll(compareList);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap, likeList);
            List<FieldInfo> orderList = CommonFunctionHelper.getOrderList(MetadataType.class);
            //CommonFunctionHelper.buildQueryWrapperOrder(queryWrapper, orderList);

            return ((List<MetadataType>) super.list(queryWrapper));
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1", e, false);
        }
    }

    /**
     * 1-1-05查询类型列表[221]
     * gen by moon at 10/15/2022, 5:37:42 PM
     */
    @Trace(operationName = "1-1-05查询类型列表")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<MetadataType> queryTypeList(QueryTypeListReq reqDto) {

        try {
            QueryWrapper<MetadataType> queryWrapper = new QueryWrapper<>();
            reqDto.setIsValid("FALSE");

            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(reqDto);
            List<FieldInfo> compareList = CommonFunctionHelper.getRangeField(MetadataType.class);
            CommonFunctionHelper.buildQueryWrapperCompare(queryWrapper, compareList, fieldInfoMap);
            List<FieldInfo> likeList = CommonFunctionHelper.getLikeFiled(MetadataType.class);
            CommonFunctionHelper.buildQueryWrapperLike(queryWrapper, likeList, fieldInfoMap);
            likeList.addAll(compareList);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap, likeList);
            List<FieldInfo> orderList = CommonFunctionHelper.getOrderList(MetadataType.class);
            //CommonFunctionHelper.buildQueryWrapperOrder(queryWrapper, orderList);
            queryWrapper.orderByDesc("operate_time");
            return ((List<MetadataType>) super.list(queryWrapper));
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1", e, false);
        }
    }

    /**
     * 1-1-05查询类型详情[24]
     * gen by moon at 10/15/2022, 5:37:42 PM
     */
    @Trace(operationName = "1-1-05查询类型详情")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public MetadataType queryTypeDetail(QueryTypeDetailReq reqDto) {

        try {
            reqDto.setIsValid("FALSE");
            QueryWrapper<MetadataType> queryWrapper = new QueryWrapper<>();
            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(reqDto);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap, new ArrayList<>());
            queryWrapper.orderByDesc("create_time");
            List<MetadataType> retList = super.list(queryWrapper);
            if (retList != null && retList.size() > 0) {
                MetadataType retData = retList.get(0);

                return retData;
            }
            return null;
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1", e, false);
        }
    }

    /**
     * 1-1-05修改类型[22]
     * gen by moon at 10/15/2022, 5:37:41 PM
     */
    @Trace(operationName = "1-1-05修改类型")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateType(MetadataType metadataType) {
        metadataType.setOperationInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//CURRENT_INDUCTIONID
        metadataType.setOperateTime(CommonFunctionHelper.getCurrentTime());//SYSTEM_CURRENT_TIME
        if (metadataType != null && metadataType.getSubjectLifeCycle() != null && metadataType.getSubjectLifeCycle().equals("CURRENTLY_VALID")) {
            metadataType.setReleaseInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//RELEASED_CURRENT_INDUCTIONID
        }
        if (metadataType != null && metadataType.getSubjectLifeCycle() != null && metadataType.getSubjectLifeCycle().equals("CURRENTLY_VALID")) {
            metadataType.setReleaseTime(CommonFunctionHelper.getCurrentTime());//RELEASE_SYSTEM_CURRENT_TIME
        }
        return super.updateById(metadataType);
    }

    /**
     * 1-1-05新增类型[21]
     * gen by moon at 10/25/2022, 9:50:39 AM
     */
    @Trace(operationName = "1-1-05新增类型")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public String addType(MetadataType metadataType) {
        metadataType.setTypeId(CommonFunctionHelper.getUid());//DISTRIBUTED_ID
        metadataType.setIsArchive("FALSE");
        metadataType.setOperationInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//CURRENT_INDUCTIONID
        metadataType.setCreateInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//CURRENT_INDUCTIONID
        if (metadataType != null && metadataType.getSubjectLifeCycle() != null && metadataType.getSubjectLifeCycle().equals("CURRENTLY_VALID")) {
            metadataType.setReleaseInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//RELEASED_CURRENT_INDUCTIONID
        }
        metadataType.setOperateTime(CommonFunctionHelper.getCurrentTime());//SYSTEM_CURRENT_TIME
        metadataType.setCreateTime(CommonFunctionHelper.getCurrentTime());//SYSTEM_CURRENT_TIME
        if (metadataType != null && metadataType.getSubjectLifeCycle() != null && metadataType.getSubjectLifeCycle().equals("CURRENTLY_VALID")) {
            metadataType.setReleaseTime(CommonFunctionHelper.getCurrentTime());//RELEASE_SYSTEM_CURRENT_TIME
        }
        metadataType.setIsValid("FALSE");
        metadataType.setSpaceId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecSpaceId());//CURRENT_SPACEID
        metadataType.setAppId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecAppId());//CURRENT_APPID
        if (super.save(metadataType)) {
            return metadataType.getTypeId();
        } else {
            return "-1";
        }

    }
}
